﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using VOL.Core.Extensions;
using VOL.Smartmanufacturing.DTO.ProductManagement;
using VOL.Smartmanufacturing.Infrastructure;
using VOL.Smartmanufacturing.IServices.ProductManagement;

namespace VOL.Smartmanufacturing.Services.ProductManagement
{
    public class MaintenanceServices : IMaintenanceServices
    {
        /// <summary>
        /// 仓储
        /// </summary>
        private readonly IBaseRepository<Entity.DomainModels.Maintenance> _maintenanceRep;
        private readonly IBaseRepository<Entity.DomainModels.TicketsLedger> _ticketsLedgerRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="maintenanceRep"></param>
        public MaintenanceServices(IBaseRepository<Entity.DomainModels.Maintenance> maintenanceRep, IBaseRepository<Entity.DomainModels.TicketsLedger> ticketsLedgerRep)
        {
            _maintenanceRep = maintenanceRep;
            _ticketsLedgerRep = ticketsLedgerRep;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="maintenance"></param>
        /// <returns></returns>
        public async Task<int> AddMaintenance(Entity.DomainModels.Maintenance maintenance)
        {
            var result = await _maintenanceRep.AddAsync(maintenance);
            return await Task.FromResult(result);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DelMaintenance(string ids)
        {
            string[] MaintenanceId = ids.Split(',');
            var result = 0;
            foreach (var item in MaintenanceId)
            {
                result = await _maintenanceRep.DelAll(m => m.Id == Convert.ToInt32(item));
            }
            return await Task.FromResult(result);
        }
        /// <summary>
        /// 回显
        /// </summary>
        /// <param name="maintenanceId"></param>
        /// <returns></returns>
        public async Task<Entity.DomainModels.Maintenance> GetMaintenanceById(int maintenanceId)
        {
            var result = await _maintenanceRep.FindAsync(maintenanceId);
            return await Task.FromResult(result);
        }
        /// <summary>
        /// 编辑 
        /// </summary>
        /// <param name="maintenance"></param>
        /// <returns></returns>
        public async Task<int> UpdateMaintenance(Entity.DomainModels.Maintenance maintenance)
        {
            var result = await _maintenanceRep.UpdateAsync(maintenance);
            return await Task.FromResult(result);
        }
        /// <summary>
        /// 显示/查询
        /// </summary>
        /// <param name="maintenanceDTO"></param>
        /// <returns></returns>
        public async Task<ApiPaging<Entity.DomainModels.Maintenance>> GetAllMaintenance(MaintenanceDTO maintenanceDTO)
        {
            var result = new ApiPaging<Entity.DomainModels.Maintenance>();
            var maintenancelist = _maintenanceRep.GetAll();
            maintenancelist = maintenancelist.WhereIF(!string.IsNullOrEmpty(maintenanceDTO.BarCode), m => m.BarCode.Contains(maintenanceDTO.BarCode));
            maintenancelist = maintenancelist.WhereIF(!string.IsNullOrEmpty(maintenanceDTO.BOMCode), m => m.BOMCode.Contains(maintenanceDTO.BOMCode));
            maintenancelist = maintenancelist.WhereIF(!string.IsNullOrEmpty(maintenanceDTO.ProductCode), m => m.ProductCode.Contains(maintenanceDTO.ProductCode));
            maintenancelist = maintenancelist.WhereIF(maintenanceDTO.StartTime.HasValue, m => m.CreateTime >= maintenanceDTO.StartTime.Value);
            maintenancelist = maintenancelist.WhereIF(maintenanceDTO.EndTime.HasValue, m => m.CreateTime <= maintenanceDTO.EndTime.Value);
            result.TotalCount = maintenancelist.Count();
            result.TotalPage = (int)Math.Ceiling(maintenancelist.Count() * 1.0 / maintenanceDTO.PageSize);
            result.Items = await maintenancelist.OrderByDescending(m => m.Id).Skip((maintenanceDTO.PageIndex - 1) * maintenanceDTO.PageSize).Take(maintenanceDTO.PageSize).ToListAsync();
            return await Task.FromResult(result);
        }

        public async Task<List<Entity.DomainModels.TicketsLedger>> GetAllTicketsLedger()
        {
            var result =  _ticketsLedgerRep.GetAll();
            return await Task.FromResult(result.ToList());
        }

    }
}
